﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RayTracing
{
    class Cube: Geometry
    {
       
        public float distance(Ray ray)
        {
            float d = Main.infinity;
            float t;
            if (ray.direction.z != 0)
            {
                t = (1 - ray.origin.z) / ray.direction.z;
                if (t > 0)
                {
                    float x = ray.origin.x + t * ray.direction.x;
                    float y = ray.origin.y + t * ray.direction.y;

                    if (Math.Abs(x) < 1 && Math.Abs(y) < 1)
                    {
                       d=Math.Min(t,d);
                    }
                }
            }
            if (ray.direction.z != 0)
            {
                t = (-1 - ray.origin.z) / ray.direction.z;
                if (t > 0)
                {
                    float x = ray.origin.x + t * ray.direction.x;
                    float y = ray.origin.y + t * ray.direction.y;

                    if (Math.Abs(x) < 1 && Math.Abs(y) < 1)
                    {
                        d = Math.Min(t, d);
                    }
                }
            }
            if (ray.direction.y != 0)
            {
                t = (1 - ray.origin.y) / ray.direction.y;
                if (t > 0)
                {
                    float x = ray.origin.x + t * ray.direction.x;
                    float z = ray.origin.z + t * ray.direction.z;

                    if (Math.Abs(x) < 1 && Math.Abs(z) < 1)
                    {
                        d = Math.Min(t, d);
                    }
                }
            }
            if (ray.direction.y != 0)
            {
                t = (-1 - ray.origin.y) / ray.direction.y;
                if (t > 0)
                {
                    float x = ray.origin.x + t * ray.direction.x;
                    float z = ray.origin.z + t * ray.direction.z;

                    if (Math.Abs(x) < 1 && Math.Abs(z) < 1)
                    {
                        d = Math.Min(t, d);
                    }
                }
            }
            if (ray.direction.x != 0)
            {
                t = (1 - ray.origin.x) / ray.direction.x;
                if (t > 0)
                {
                    float z = ray.origin.z + t * ray.direction.z;
                    float y = ray.origin.y + t * ray.direction.y;

                    if (Math.Abs(y) < 1 && Math.Abs(z) < 1)
                    {
                        d = Math.Min(t, d);
                    }
                }
            }
            if (ray.direction.x != 0)
            {
                t = (-1 - ray.origin.x) / ray.direction.x;
                if (t > 0)
                {
                    float z = ray.origin.z + t * ray.direction.z;
                    float y = ray.origin.y + t * ray.direction.y;

                    if (Math.Abs(y) < 1 && Math.Abs(z) < 1)
                    {
                        d = Math.Min(t, d);
                    }
                }
            }
            return d;
        }

        public P3 normal(P3 p)
        {
           
            if (p.z > .999)
            {
                return new P3(0, 0, 1);
            }
            if (p.z < -.999)
            {
                return new P3(0, 0, -1);
            }
            if (p.y > .999)
            {
                return new P3(0, 1,0);
            }
            if (p.y < -.999)
            {
                return new P3(0, -1,0);
            }
            if (p.x > .999)
            {
                return new P3(1,0,0);
            }
            else
            {
                return new P3(-1,0,0);
            }
        }
        public P2 toP2(P3 p)
        {
           
            if ((p.z) > .999)
            {
                return new P2((p.x + 1) / 2, (p.y + 1) / 2,5);
            }
            if ((p.z) <-.999)
            {
                return new P2((p.x + 1) / 2, (p.y + 1) / 2, 4);
            }
            if ((p.x) > .999)
            {
                return new P2((p.z + 1)/2, (p.y + 1)/2,0);
            }
            if ((p.x) <-.999)
            {
                return new P2((p.z + 1) / 2, (p.y + 1) / 2,2);
            }
            if ((p.y) > .999)
            {
                return new P2((p.z + 1) / 2, (p.y + 1) / 2,1);
            }
            else
            {
                return new P2((p.x + 1) / 2, (p.z + 1) / 2,3);
            }
        }
    }
}
